
#include <iostream>
#include <cstdlib>
#include <assert.h>

#include "corbaAddressServer.h"
#include "CServiceSM.h"
#include "../tools/log.h"
#include <boost/concept_check.hpp>

using namespace std;

int main (int argc,char** args)

{

    cout <<"mcp_test"<<endl;

    try {
        //Initialize CORBA ORB - "orb"
        CORBA::ORB_var orb=CORBA::ORB_init(argc,args);


        CORBA::Object_var POAObj=orb->resolve_initial_references("RootPOA");
        PortableServer::POA_var _poa=PortableServer::POA::_narrow(POAObj.in());

        CORBA::Object_var nsobj =orb->resolve_initial_references("NameService");

        CosNaming::NamingContext_var initnc =CosNaming::NamingContext::_narrow(nsobj);
        try {

            CosNaming::Name name;

            name.length(2);
            name[0].id = CORBA::string_dup("AddressServer");
            name[0].kind = CORBA::string_dup("");
            name[1].id = CORBA::string_dup("SM");
            name[1].kind = CORBA::string_dup("");

            cout<<"Resolving sm object"<<endl;

// Name aufloesen
            CORBA::Object_var obj = initnc->resolve(name);


            cout <<"Found sm object"<<endl;

// Objektzeiger in Accountzeiger umwandeln
            AddressServer::sm_var SM = AddressServer::sm::_narrow(obj);


            for (int i=0;i<100;i++)
            {
                char ch[255];
                sprintf( ch,"IOR:test%d", i);
                ::CORBA::ULong id=SM->registerSM(ch);
                cout <<"received id:" <<id<<" and URL:"<<ch<<endl;
            }
//			cout <<SM->unregisterSM(id)<<endl;

        }
        catch (CosNaming::NamingContext::NotFound & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::notFoundException."<<endl;
        }
        catch (CosNaming::NamingContext::CannotProceed & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::CannotProceed."<<endl;
        }
        catch (CosNaming::NamingContext::InvalidName & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::InvalidName."<<endl;
        }
        catch (CosNaming::NamingContext::AlreadyBound & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::AlreadyBound."<<endl;
        }
        catch (CosNaming::NamingContext::NotEmpty & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::NotEmpty."<<endl;
        }





    }
    catch (CORBA::SystemException& se)
    {
        FILE_LOG(logERROR)<<"Caught CORBA::SystemException. "<<endl;
    }
    catch (CORBA::Exception&)
    {
        FILE_LOG(logERROR)<<"Caught CORBA::Exception."<<endl;
    }
    catch (omniORB::fatalException& fe)
    {
        FILE_LOG(logERROR)<<"Caught omniORB::fatalException:"<<endl;
        FILE_LOG(logERROR)<<" file: "<<fe.file()<<endl;
        FILE_LOG(logERROR)<<" line: "<<fe.line()<<endl;
        FILE_LOG(logERROR)<<" mesg: "<<fe.errmsg()<<endl;
    }
    catch (...)
    {
        FILE_LOG(logERROR)<<"Caught unknown exception."<<endl;
    }
    return 0;
}
